/**
 * 
 */
package com.dycn.posdashboard.data;

import com.dycn.posdashboard.util.MD5Support;
import com.dycn.posdashboard.util.StaticUitls;
/**
 * @author gongqiyi
 * 
 */
public class DataSourceConfig implements java.io.Serializable {

	/**
	 * 
	 */
	private static final long serialVersionUID = 35071946293588271L;

	private String dbLoginUser;
	private String dbLoginPwd;
	private String dbIpAddress;
	private String dbListenPort;
	private String dbName;
	private String dbType;// ORACLE,MYSQL,TD
	private String otherParamaters;


	private String createUniqueDBSourceKey() {
		String orgMsg = "LU:" + this.getDbLoginUser() + "PW:"
				+ this.getDbLoginPwd() + "IP:" + this.getDbIpAddress() + "PO:"
				+ this.getDbListenPort() + "DB:" + this.getDbName() + "TP:"
				+ this.getDbType() + "OT:" + this.getOtherParamaters();
		return "dbSource" + MD5Support.MD5(orgMsg);
	}

	private String getConnetionURL(String ID) {
		/**各数据库的URL
		 * oracle：
		 * jdbc:oracle:thin:@localhost:1521:orcl
		 * teradata：
		 * td_url=jdbc:teradata://10.1.0.188/CLIENT_CHARSET=cp936,TMODE=TERA,CHARSET=ASCII,DATABASE=YHD_ADMIN
		 * mysql：
		 * jdbc:mysql://localhost:3306/sample_db?user=root&password=your_password
		*/
		/*
		IBaseDao dao = new BaseDaoImpl();
		MetaModelSource mms = dao.getMetaSourceById(ID);
		String url= "";
		//Long id;
		dbLoginUser = mms.getDbLoginUser();
		dbLoginPwd = mms.getDbLoginPwd();
		dbIpAddress = mms.getDbIpAddress();
		dbListenPort = mms.getDbListenPort();
		dbName = mms.getDbName();
		dbType = mms.getDbType();
		otherParamaters = mms.getOtherParamaters();
		*/
		String url= "";
		if(StaticUitls.ORACLE.equals(dbType) || StaticUitls.ORACLE.equalsIgnoreCase(dbType)){
			String oraURL = "jdbc:oracle:thin:@"+dbIpAddress+":"+dbListenPort+":"+dbName;
			url = oraURL;
		}else if (StaticUitls.TERADATA.equals(dbType) || StaticUitls.TERADATA.equalsIgnoreCase(dbType)){
			String tdURL = "jdbc:teradata://"+dbIpAddress+
			//端口 default 1025
					//CLIENT_CHARSET=UTF8,TMODE=TERA
					//中文乱码问题
			((null==dbListenPort||"".equals(dbListenPort))?("/"):("/DBS_PORT="+dbListenPort+","))+
			""+"CLIENT_CHARSET=EUC_CN,TMODE=TERA,CHARSET=ASCII,DATABASE="+dbName;
			url = tdURL;
		}else if (StaticUitls.MYSQL .equals( dbType) || StaticUitls.MYSQL.equalsIgnoreCase(dbType)){
			String mysqlURL = "jdbc:mysql://"+dbIpAddress+
			//端口
			((null==dbListenPort||"".equals(dbListenPort))?(""):(":"+dbListenPort))+
			"/"+dbName+
			"?user="+dbLoginUser+"&password="+dbLoginUser;
			url = mysqlURL;
		}else{
			url = "jdbc:oracle:thin:@localhost:1521:orcl";
		}
		return url;
	}

	public String getConnetionURL() {
		String idStr = this.getSourceKey();
		//Long id = Long.parseLong(idStr);
		String url = getConnetionURL(idStr);
		return url;
	}

	public String getDriverClassName() {
		if (dbType != null && !dbType.trim().equals("")) {
			if (dbType.trim().equals(StaticUitls.ORACLE)) {
				return StaticUitls.ORALCE_JDBC_DRIVE_CLASS_NAME;
			} else if (dbType.trim().equals(StaticUitls.MYSQL)) {
				return StaticUitls.MYYSQL_JDBC_DRIVE_CLASS_NAME;
			} else if (dbType.trim().equals(StaticUitls.TD)) {
				return StaticUitls.TD_JDBC_DRIVE_CLASS_NAME;
			} else {
				return StaticUitls.ORALCE_JDBC_DRIVE_CLASS_NAME;
			}
		} else {
			return StaticUitls.ORALCE_JDBC_DRIVE_CLASS_NAME;
		}

	}
	
	public String getValidationQuery(){
		if (dbType != null && !dbType.trim().equals("")) {
			if (dbType.trim().equals(StaticUitls.ORACLE)) {
				return StaticUitls.ORALCE_JDBC_validationQuery;
			} else if (dbType.trim().equals(StaticUitls.MYSQL)) {
				return StaticUitls.MYYSQL_JDBC_validationQuery;
			} else if (dbType.trim().equals(StaticUitls.TD)) {
				return StaticUitls.TD_JDBC_validationQuery;
			} else {
				return StaticUitls.ORALCE_JDBC_DRIVE_CLASS_NAME;
			}
		} else {
			return StaticUitls.ORALCE_JDBC_validationQuery;
		}
	}

	public String getSourceKey() {
		return createUniqueDBSourceKey();
	}

	public String getDbLoginUser() {
		return dbLoginUser;
	}

	public void setDbLoginUser(String dbLoginUser) {
		this.dbLoginUser = dbLoginUser;
	}

	public String getDbLoginPwd() {
		return dbLoginPwd;
	}

	public void setDbLoginPwd(String dbLoginPwd) {
		this.dbLoginPwd = dbLoginPwd;
	}

	public String getDbIpAddress() {
		return dbIpAddress;
	}

	public void setDbIpAddress(String dbIpAddress) {
		this.dbIpAddress = dbIpAddress;
	}

	public String getDbListenPort() {
		return dbListenPort;
	}

	public void setDbListenPort(String dbListenPort) {
		this.dbListenPort = dbListenPort;
	}

	public String getDbName() {
		return dbName;
	}

	public void setDbName(String dbName) {
		this.dbName = dbName;
	}

	public String getDbType() {
		return dbType;
	}

	public void setDbType(String dbType) {
		this.dbType = dbType;
	}

	public String getOtherParamaters() {
		return otherParamaters;
	}

	public void setOtherParamaters(String otherParamaters) {
		this.otherParamaters = otherParamaters;
	}

}
